//
//  V4SelectView.m
//  XinKaiFa55like
//
//  Created by 55like on 23/09/2017.
//  Copyright © 2017 55like lj. All rights reserved.
//

#import "V4ShareShowView.h"



#import <ShareSDK/ShareSDK.h>
#import "WXApi.h"
#import <ShareSDKConnector/ShareSDKConnector.h>

#import <ShareSDKExtension/ShareSDK+Extension.h>
#import <MessageUI/MFMailComposeViewController.h>
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
#import "WSButtonGroup.h"
@interface V4ShareShowView ()<WSButtonGroupdelegate,UITextFieldDelegate>
{
    
    UIScrollView*scrollView;
    WSButtonGroup*_sortBtnGroup;
    WSButtonGroup*_levelBtnGroup;
    WSButtonGroup*_classBtnGroup;
    WSButtonGroup*_yearBtnGroup;
    
    UITextField*_tflow;
    UITextField*_tfHeigh;
    
    
}
    @property(nonatomic,weak)UIView*contentView;
    @property(nonatomic,weak)UIView*myeventBtn;
@property(nonatomic,strong)NSDictionary*shareDatamap;
@property(nonatomic,strong)NSDictionary*collectionDic;

@end

@implementation V4ShareShowView
#pragma mark  开始
+(V4ShareShowView*)showMeWithShareDic:(NSDictionary*)ShareDic WithCollectiondic:(NSDictionary*)collectionDic CallBackBlock:(AllcallBlock) callBackBlock{
    V4ShareShowView*view=[UTILITY.currentViewController getAddValueForKey:@"V4SelectView"];
    if (!view) {
        view=[[V4ShareShowView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        [UTILITY.currentViewController.view addSubview:view];
        
        view.shareDatamap=ShareDic;
        view.collectionDic=collectionDic;

        [UTILITY.currentViewController setAddValue:view forKey:@"V4SelectView"];
        [view addView];
    }
    
    view.callBackBlock=callBackBlock;
    view.hidden=NO;
    view.contentView.frameY=view.frameHeight;
    view.myeventBtn.alpha=0;
    [UIView animateWithDuration:0.2 animations:^{
        view.contentView.frameBY=0;
        view.myeventBtn.alpha=1;
    } completion:^(BOOL finished) {
        
    }];
 
    
    return view;
}

#pragma mark -  写UI
-(void)addView{
    {
        
        UIButton *btn=[[UIButton alloc]initWithFrame:self.bounds];
        _myeventBtn=btn;
        [self addSubview:btn];
        btn.backgroundColor=RGBACOLOR(127, 127, 127, 0.5);
        [btn addTarget:self action:@selector(fcloseme) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIView*viewContent=[RHMethods viewWithFrame:CGRectMake(0, kScreenHeight-146-50, kScreenWidth, 146+50) backgroundcolor:RGBACOLOR(232, 232, 232, 0.98) superView:self];
    [RHMethods ClableY:9 W:kScreenWidth Height:15 font:13 superview:viewContent withColor:RGBACOLOR(94, 94, 94, 1) text:@"分享到"];
    _contentView=viewContent;
    //    NSArray*arraytitle=@[@"微信好友",@"朋友圈",@"邮件",@"收藏",];
    NSMutableArray*arraytitle=[NSMutableArray new];
    BOOL isInstallWx = [ShareSDK isClientInstalled:SSDKPlatformTypeWechat];
    if (isInstallWx) {
        [arraytitle addObject:@"微信好友"];
        [arraytitle addObject:@"朋友圈"];
    }
    [arraytitle addObject:@"邮件"];
    if (_collectionDic) {
        [arraytitle addObject:@"收藏"];
    }
    
    float cellMargn=(kScreenWidth-4*60)/(4+1);
    
    for (int i=0; i<arraytitle.count; i++) {
        NSString*strTitle=arraytitle[i];
         UIView*viewBg=[RHMethods viewWithFrame:CGRectMake(i*(cellMargn+60)+cellMargn, 46, 60, 60) backgroundcolor:RGBACOLOR(249, 249, 249, 1) superView:viewContent];
        WSSizeButton*btn1=[RHMethods buttonWithframe:CGRectMake(i*(cellMargn+60)+cellMargn, 46, 60, 87) backgroundColor:nil text:strTitle font:12 textColor:rgb(79,79,79) radius:0 superview:viewContent];
       
        viewBg.layer.cornerRadius=10;
        [btn1 setBtnImageViewFrame:CGRectMake((60-31)/2, (60-31)/2, 31, 31)];
        [btn1 setBtnLableFrame:CGRectMake(0, 60, 60, 33)];
        btn1.titleLabel.textAlignment=NSTextAlignmentCenter;
        btn1.titleLabel.font=Font(13);
        
        [btn1 addTarget:self action:@selector(OkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        if ([strTitle isEqualToString:@"微信好友"]) {
            [btn1 setImage:[UIImage imageNamed:@"xkf_shareicon01"] forState:UIControlStateNormal];
        }else if ([strTitle isEqualToString:@"朋友圈"]) {
            [btn1 setImage:[UIImage imageNamed:@"xkf_shareicon02"] forState:UIControlStateNormal];
        }else if ([strTitle isEqualToString:@"邮件"]) {
            [btn1 setImage:[UIImage imageNamed:@"xkf_shareicon03"] forState:UIControlStateNormal];
        }else if ([strTitle isEqualToString:@"收藏"]) {
            [btn1 setImage:[UIImage imageNamed:@"xkf_shareicon04"] forState:UIControlStateNormal];
            [btn1 setImage:[UIImage imageNamed:@"xkf_shareicon05"] forState:UIControlStateSelected];
            [btn1 setTitle:@"取消收藏" forState:UIControlStateSelected];
            [btn1 removeAllTargets];
            btn1.selected=[[_collectionDic valueForJSONStrKey:@"iscollection"] isEqualToString:@"1"];
            [btn1 addTarget:self action:@selector(shouchang:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        
        
        btn1.imageView.contentMode=UIViewContentModeScaleAspectFit;
    }
//    [RHMethods viewWithFrame:CGRectMake(10, 146, kScreenWidth-20, 1) backgroundcolor:rgb(194,194,194) superView:viewContent];
    WSSizeButton*btnCancell=[RHMethods buttonWithframe:CGRectMake(0, viewContent.frameHeight-50, kScreenWidth, 50) backgroundColor:rgb(244, 244, 244) text:@"取消" font:17 textColor:rgb(53, 53, 53) radius:0 superview:viewContent];
    [btnCancell addTarget:self action:@selector(closeme) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
}
-(void)addView2{
    {
        
        UIButton *btn=[[UIButton alloc]initWithFrame:self.bounds];
        [self addSubview:btn];
        btn.backgroundColor=RGBACOLOR(127, 127, 127, 0.5);
        [btn addTarget:self action:@selector(fcloseme) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIView*viewContent=[RHMethods viewWithFrame:CGRectMake(0, kScreenHeight-195, kScreenWidth, 195) backgroundcolor:zrgbwhiteColor superView:self];
    [RHMethods ClableY:15 W:kScreenWidth Height:15 font:15 superview:viewContent withColor:nil text:@"分享到"];
    
    //    NSArray*arraytitle=@[@"微信好友",@"朋友圈",@"邮件",@"收藏",];
    NSMutableArray*arraytitle=[NSMutableArray new];
    BOOL isInstallWx = [ShareSDK isClientInstalled:SSDKPlatformTypeWechat];
    if (isInstallWx) {
        [arraytitle addObject:@"微信好友"];
        [arraytitle addObject:@"朋友圈"];
    }
    [arraytitle addObject:@"邮件"];
    if (_collectionDic) {
        [arraytitle addObject:@"收藏"];
    }
    
    float cellMargn=((kScreenWidth-20)-4*50)/(4-1);
    
    for (int i=0; i<arraytitle.count; i++) {
        NSString*strTitle=arraytitle[i];
        WSSizeButton*btn1=[RHMethods buttonWithframe:CGRectMake(10+i*(cellMargn+50), 60, 50, 87) backgroundColor:nil text:strTitle font:12 textColor:rgb(153,153,153) radius:0 superview:viewContent];
        [btn1 setBtnImageViewFrame:CGRectMake(0, 0, 50, 36)];
        [btn1 setBtnLableFrame:CGRectMake(0, 45, 50, 20)];
        btn1.titleLabel.textAlignment=NSTextAlignmentCenter;
        btn1.titleLabel.font=Font(12);
        
        [btn1 addTarget:self action:@selector(OkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        if ([strTitle isEqualToString:@"微信好友"]) {
            [btn1 setImage:[UIImage imageNamed:@"xkf_shareicon01"] forState:UIControlStateNormal];
        }else if ([strTitle isEqualToString:@"朋友圈"]) {
            [btn1 setImage:[UIImage imageNamed:@"xkf_shareicon02"] forState:UIControlStateNormal];
        }else if ([strTitle isEqualToString:@"邮件"]) {
            [btn1 setImage:[UIImage imageNamed:@"xkf_shareicon03"] forState:UIControlStateNormal];
        }else if ([strTitle isEqualToString:@"收藏"]) {
            [btn1 setImage:[UIImage imageNamed:@"xkf_shareicon04"] forState:UIControlStateNormal];
            [btn1 setImage:[UIImage imageNamed:@"xkf_shareicon05"] forState:UIControlStateSelected];
            [btn1 setTitle:@"取消收藏" forState:UIControlStateSelected];
            [btn1 removeAllTargets];
            btn1.selected=[[_collectionDic valueForJSONStrKey:@"iscollection"] isEqualToString:@"1"];
            [btn1 addTarget:self action:@selector(shouchang:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        
        
        btn1.imageView.contentMode=UIViewContentModeCenter;
    }
    [RHMethods viewWithFrame:CGRectMake(10, 146, kScreenWidth-20, 1) backgroundcolor:rgb(194,194,194) superView:viewContent];
    WSSizeButton*btnCancell=[RHMethods buttonWithframe:CGRectMake(0, 147, kScreenWidth, viewContent.frameHeight-144) backgroundColor:nil text:@"取消" font:18 textColor:rgb(223,171,34) radius:0 superview:viewContent];
    [btnCancell addTarget:self action:@selector(closeme) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
}


#pragma mark - 请求数据
-(void)shouchang:(UIButton*)btn{
    
    krequestParam
    if (_collectionDic) {
        
        [dictparam addEntriesFromDictionary:_collectionDic];
    }
    
    [ dictparam setObject:btn.selected?@"0":@"1" forKey:@"act"];
    
    [NetEngine createGetAction_LJ:[NSString stringWithFormat:@"homes/collection%@",dictparam.wgetParamStr] onCompletion:^(id resData, BOOL isCache) {
                if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
                    [SVProgressHUD showImage:nil status:[resData objectForJSONKey:@"info"]];
                    btn.selected=!btn.selected;
                    [self closeme];
                    self.callBackBlock(@"收藏",200,nil);
        
                }else{
                    
                    [SVProgressHUD showImage:nil status:[resData objectForJSONKey:@"info"]];
                }
    }];
    
    
}

//-(void)email{
//
//
//    //如果不能发送邮件就返回
//    if (![MFMailComposeViewController canSendMail]) {
//        return;
//    }
//    MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc]init];
//    //收件人列表
////    [mailVC setToRecipients:@[@"drenhart@163.com",@"12222@163.com"]];
//    //抄送
////    [mailVC setCcRecipients:@[@"123144@163.com"]];
////    //密送
////    [mailVC setBccRecipients:@[@"987676@163.com"]];
//    //主题
//    [mailVC setSubject:[_shareDatamap objectForJSONKey:@"title"]];
//    
//    
//    NSString*url=[_shareDatamap objectForJSONKey:@"src"];
//    [url rangeOfString:@"http://"].length?url:[NSString stringWithFormat:@"http://%@%@",basePicPath,url];
//    //内容
//    [mailVC setMessageBody:@"推荐使用xxx应用 wwww.aaaaa.com" isHTML:NO];
//    //附件
//    UIImage *img = [UIImage imageNamed:@"rockstar programmer"];
//    NSData *data = UIImagePNGRepresentation(img);
//    [mailVC addAttachmentData:data mimeType:@"image/jpg" fileName:@"rockstar programmer.jpg"];
//    //MFMailComposeViewConreoller的代理
//    mailVC.mailComposeDelegate = self;
//    [UTILITY.currentViewController presentViewController:mailVC animated:YES completion:nil];
//
//}
//- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
//    [UTILITY.currentViewController dismissViewControllerAnimated:YES completion:nil];
//}
#pragma mark - 事件监听

-(void)OkBtnClick:(UIButton*)btn{
 
//    NSArray*titlArray=@[@"微信好友",@"微信朋友圈",@"QQ好友",@"QQ空间",@"新浪微博",];
//    NSArray*titlArray2=@[@"wx",@"wxfriend",@"qq",@"qq",@"wb",];
//    NSString*titlestr=titlArray[btn.tag-1000];
//    NSString*typstr=titlArray2[btn.tag-1000];
    
    NSString*titlestr=btn.currentTitle;
    
    SSDKPlatformType typexx;
    if ([titlestr isEqualToString:@"微信好友"]) {
        typexx=SSDKPlatformTypeWechat;
        
    }else if ([titlestr isEqualToString:@"朋友圈"]) {
        typexx=SSDKPlatformSubTypeWechatTimeline;
    }else if ([titlestr isEqualToString:@"邮件"]) {
        typexx=SSDKPlatformTypeMail;
    }
    NSString*url=[_shareDatamap objectForJSONKey:@"src"];
    [url rangeOfString:@"http://"].length?url:[NSString stringWithFormat:@"http://%@%@",basePicPath,url];
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    
    
    if ([titlestr isEqualToString:@"邮件"]) {
        [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@\n%@",[_shareDatamap objectForJSONKey:@"title"],[_shareDatamap objectForJSONKey:@"url"]]
                                         images:url//传入要分享的图片
                                            url:[NSURL URLWithString:[_shareDatamap objectForJSONKey:@"url"]]
                                          title:[_shareDatamap objectForJSONKey:@"title"]
                                           type:SSDKContentTypeAuto];
        //        [_shareDatamap objectForJSONKey:@"title"]
    }else{
        
        [shareParams SSDKSetupShareParamsByText:[_shareDatamap objectForJSONKey:@"descr"]
                                         images:url//传入要分享的图片
                                            url:[NSURL URLWithString:[_shareDatamap objectForJSONKey:@"url"]]
                                          title:[_shareDatamap objectForJSONKey:@"title"]
                                           type:SSDKContentTypeWebPage];
        
    }
    //进行分享
    [ShareSDK share:typexx //传入分享的平台类型
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
         
         if(state==SSDKResponseStateSuccess){
             [self closeme];
             _callBackBlock(titlestr,200,nil);
         }else if (state==SSDKResponseStateFail){
             
             if (typexx==SSDKPlatformTypeMail) {
                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                 message:[NSString stringWithFormat:@"%@",@"邮件分享需要系统邮箱登录才能分享成功"]
                                                                delegate:nil
                                                       cancelButtonTitle:@"OK"
                                                       otherButtonTitles:nil, nil];
                 [alert show];
             }else{
                 DLog(@"\n\nerror:%@\n\n",error);
                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                 message:[NSString stringWithFormat:@"%@",error]
                                                                delegate:nil
                                                       cancelButtonTitle:@"OK"
                                                       otherButtonTitles:nil, nil];
                 [alert show];
             
             }
             
//             self.shareSuc?self.shareSuc(0):nil;
//             self.shareSuc = nil;
         }else if(state==SSDKResponseStateCancel){
//             [SVProgressHUD showImage:nil status:@"您取消了分享"];
         }
         
         // 回调处理....}];
     }];
}

-(void)fcloseme{
    if (self.callBackBlock) {
        self.callBackBlock(nil,201,nil);
    }
    
    [self closeme];
}
-(void)closeme{
    V4ShareShowView*view=self;
    
    view.contentView.frameBY=0;
    view.myeventBtn.alpha=1;
    [UIView animateWithDuration:0.2 animations:^{
        
        view.contentView.frameY=view.frameHeight;
        view.myeventBtn.alpha=0;
    } completion:^(BOOL finished) {
        
        self.hidden=YES;
    }];
    
    
    
    
}

#pragma mark  单击后键盘消失

@end

